package gam.n1.cm.i;

import gam.n1.areas.Area;
import gam.n1.areas.AreaManager;
import gam.n1.ask.Asking;
import gam.n1.conf.ConfManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.concurrent.TimeUnit;

public class AreaCreateHandler extends BaseHandler {
    public AreaCreateHandler(Plugin plugin) {
        super(plugin);
    }

    @Override
    public boolean handle(Player player, String[] args) throws Exception {
        if (player != null){
            if (args.length == 2){
                String key = args[0];
                String aN = args[1];
                Area _bArea = AreaManager.Build(plugin, key, aN);
                if (_bArea != null){
                    if (!Asking.isAsking(player.getUniqueId())){
                        Asking.Ask(player.getUniqueId(),null);
                        Bukkit.getScheduler().runTaskAsynchronously(plugin,()->{
                            try {
                                Area var1 = _bArea.aH(player).get(3, TimeUnit.MINUTES);
                                if (var1 != null) {
                                    AreaManager.addArea(var1);
                                    ConfManager.Msg.send(player,ConfManager.Msg.getString("area.create.success"),"{area}",var1.getAreaName());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                ConfManager.Msg.send(player,ConfManager.Msg.getString("area.create.set.point.field"),null,null);
                                ConfManager.Msg.send(player,ConfManager.Msg.getString("area.create.error.field"),null,null);
                            } finally {
                                Asking.remove(player.getUniqueId());
                            }
                        });
                    } else ConfManager.Msg.send(player,ConfManager.Msg.getString("area.create.error.asking"),"{ask}",Asking.getAskingArea(player.getUniqueId()).getAreaName());
                } else ConfManager.Msg.send(player,ConfManager.Msg.getString("area.create.error.unknownType"),"{area}",key);
            } else ConfManager.Msg.send(player,ConfManager.Msg.getString("area.command.wrong"),null,null);
        }
        return false;
    }
}
